﻿using Home.Domain.Entities;
using System;
using System.Data.Entity;
using System.Linq;
using System.Web.Mvc;

namespace Home.WebUI.Areas.ChmNote.Controllers
{
    public class HomeController : Controller
    {
        #region 基本
        private EFDbContext db = new EFDbContext();
        protected override void Dispose(bool disposing)
        {
            db.Dispose();
            base.Dispose(disposing);
        }
        #endregion

        //
        // GET: /ChmNote/Home/

        public ActionResult Index()
        {
            return View();
        }

        public ActionResult ArticleList(int id)
        {
            var model = db.PArticles.Find(id);
            return View(model);
        }

        /// <summary>
        /// 获取文章
        /// </summary>
        /// <param name="id">文章Article的ID</param>
        /// <returns></returns>
        public ActionResult Article(int id = 0)
        {
            var model = db.Articles.First(t => t.Id == id);
            return View(model);
        }

        public ActionResult AddArticle(int id)
        {
            var model = new Article();
            model.Navbar = id;//无用
            model.PArticleId = id;
            return View(model);
        }
        [HttpPost]
        [ValidateInput(false)]
        public ActionResult AddArticle(Article model)
        {

            model.Navbar = 6;
            model.CreateDate = DateTime.Now;
            model.EditDate = DateTime.Now;
            int sort = db.Articles.Select(p => p.Sort).DefaultIfEmpty(0).Max();
            model.Sort = sort + 1;
            db.Articles.Add(model);
            db.SaveChanges();
            return RedirectToAction("ArticleList", new { id = model.PArticleId });
        }

        public ActionResult EditArticle(int id)
        {
            var model = db.Articles.Find(id);
            return View(model);
        }
        [HttpPost]
        [ValidateInput(false)]
        public ActionResult EditArticle(Article model)
        {
            var content = db.Articles.Find(model.Id);
            content.EditDate = DateTime.Now;
            content.Title = model.Title;
            content.Body = model.Body;
            db.SaveChanges();
            return RedirectToAction("Article", new { id = model.Id });
        }
        public ActionResult ChaJian()
        {
            var model = db.PArticles.OrderBy(t => t.Sort).Where(t => t.Navbar == 5).ToList();
            return View(model);
        }

        public ActionResult AddPArticle(PArticle model)
        {
            if (Request.IsAjaxRequest())
            {
                model.CreateDate = DateTime.Now;
                model.EditDate = DateTime.Now;
                int sort = db.PArticles.Select(p => p.Sort).DefaultIfEmpty(0).Max();
                model.Sort = sort + 1;
                db.PArticles.Add(model);
                db.SaveChanges();
                return Content("1");
            }
            else
            {
                return Content("-1");
            }
        }

        public ActionResult EditPArticle(int id)
        {
            return View(db.PArticles.Find(id));
        }
        [HttpPost]
        public ActionResult EditPArticle(PArticle model)
        {
            if (Request.IsAjaxRequest())
            {
                var content = db.PArticles.Find(model.Id);
                content.Title = model.Title;
                content.EditDate = DateTime.Now;
                db.SaveChanges();
                return Content("1");
            }
            else
            {
                return Content("-1");
            }
        }
    }
}
